package com.springboot.rmq.comm;

import com.springboot.rmq.producer.manager.MsgProducerManager;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Optional;
import java.util.Properties;

@Configuration
@EnableConfigurationProperties(ProducerProperties.class)
public class ProducerConfig {
    /**
     * Producer properties
     */
    private ProducerProperties properties;

    /**
     * 通过构造方法注入
     *
     * @param properties properties
     */
    public ProducerConfig(ProducerProperties properties) {
        this.properties = properties;
    }


    /**
     * 获取producerProp配置文件
     */
    @Bean(name = "producerManager")
    public MsgProducerManager getProducerProp() {

        Properties prop = new Properties();
        //设置nameserver属性
        prop.setProperty(Constant.KEY_PRODUCER_NAMESERVER, this.properties.getNameServer());
        //设置groupname
        prop.setProperty(Constant.KEY_PRODUCER_GROUPNAME, this.properties.getGroupName());

        //设置topicqueuenum
        //prop.setProperty(Constant.KEY_PRODUCER_TOPICQUEUENUMS, this.properties.getTopicQueueNum());
        prop.setProperty(Constant.KEY_PRODUCER_TOPICQUEUENUMS, String.valueOf(1111));

        //设置timeout
        prop.setProperty(Constant.KEY_PRODUCER_TIMEOUT, this.properties.getTimeOutMils());
        //设置instancename
        prop.setProperty(Constant.KEY_PRODUCER_INSTANCENAME, this.properties.getInstanceName());

        //设置同步发送失败重发次数
        String retryTimesWhenSendFailed = this.properties.getRetryTimesWhenSendFailed();
        if (retryTimesWhenSendFailed == null){
            retryTimesWhenSendFailed ="3";
        }
        prop.setProperty(Constant.KEY_PRODUCER_RETRYSYNC_TIMES,retryTimesWhenSendFailed );

        //设置异步发送失败重发次数
        String retryTimesWhenSendAsyncFailed = this.properties.getRetryTimesWhenSendAsyncFailed();
        if (retryTimesWhenSendAsyncFailed == null){
            retryTimesWhenSendAsyncFailed ="3";
        }
        prop.setProperty(Constant.KEY_PRODUCER_RETRYASYNC_TIMES,retryTimesWhenSendAsyncFailed );

        // 设置VIP频道发送消息
        String sendMessageWithVIPChannel = this.properties.getSendMessageWithVIPChannel();
        if (sendMessageWithVIPChannel == null) {
            sendMessageWithVIPChannel = "true";
        }
        prop.setProperty(Constant.KEY_SEND_MSG_WITH_VIP_CHANNEL, sendMessageWithVIPChannel);
        //return this.properties.get
        MsgProducerManager producerManager = new MsgProducerManager();
        producerManager.setProducerProp(prop);
        return producerManager;
    }


}
